/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.eclipse.jadex.adfmanager.wizards;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ResourceSelectionDialog;

/**
 * This page allows to get the following information in order to create a new reference to an existing capability
 *  - Capability name
 *  - Capability File
 *    
 *  @author Pablo Muñoz
 */

public class AddCapabilityReferenceWizardPage extends WizardPage {
	private Text capabilityNameText;
	private Text capabilityFileText;

	/**
	 * Constructor for AgentWizardPage
	 * 
	 * @param pageName
	 */
	public AddCapabilityReferenceWizardPage(ISelection selection) {
		super("wizardPage");
		setTitle("Capability");
		setDescription("This wizard adds a reference to an existing capability in the selected Agent");
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 3;
		layout.verticalSpacing = 9;
		
		Label label = new Label(container, SWT.NULL);
		label.setText("Capability name:");
		capabilityNameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		capabilityNameText.setLayoutData(gd);
		capabilityNameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label label2 = new Label(container, SWT.NULL);
		label2.setText("Capability definition file:");
		capabilityFileText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		capabilityFileText.setLayoutData(gd);
		capabilityFileText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		Button button = new Button(container, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e) {
				handleBrowse();
			}
		});
		initialize();
		dialogChanged();
		setControl(container);
	}

	/**
	 * Tests if the current workbench selection is a suitable container to use.
	 */
	private void initialize() {
	}

	/**
	 * Ensures that the all text fields are set correctly.
	 */
	private void dialogChanged() {
		File file = new File(getCapabilityFilePath());
		if(capabilityNameText.getText().length() == 0){
			updateStatus("capability name must be set");
			return;
		}
		if(capabilityFileText.getText().length() == 0){
			updateStatus("capability file must be set");
			return;
		}
		if(file != null && !file.exists()){
			updateStatus("The selected capability file doesn't exist");
			return;
		}
		if(!file.getName().endsWith("capability.xml")){
			updateStatus("Capability files must have the \"capability.xml\" extension");
			return;
		}
		updateStatus(null);
	}

	/*
	 * This method is called in order to update the status message 
	 */
	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}
	
	
	/**
	 * 
	 * @return the capability name
	 */
	public String getCapabilityName() {
		return capabilityNameText.getText();
	}

	/**
	 * 
	 * @return the path to the capability definition file
	 */
	public String getCapabilityFilePath() {
		return capabilityFileText.getText();
	}

	private void handleBrowse() {
		ResourceSelectionDialog dialog = new ResourceSelectionDialog(
				getShell(), ResourcesPlugin.getWorkspace().getRoot(),
				"Select capability definition file");
		if (dialog.open() == ResourceSelectionDialog.OK) {
			Object[] result = dialog.getResult();
			if (result.length == 1) {
				capabilityFileText.setText(((IFile)result[0]).getRawLocation().toOSString());
				dialogChanged();
			}
		}
	}
}